#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>

#define NUM 1024
#define OPT_NUM 64

char lineCommand[NUM];
char *myargv[OPT_NUM];

int main()
{
    while(1)
    {
        //输出提示符
        printf("用户名@主机号 当前路径# ");
        fflush(stdout);

        //获取用户输入,输入的时候输入\n
        char *s = fgets(lineCommand, sizeof(lineCommand)-1, stdin);
        assert(s != NULL);
        (void)s;
        //清除最后一个\n
        lineCommand[strlen(lineCommand)-1] = 0;

        //字符串切割
        myargv[0] = strtok(lineCommand, " ");
        int i=1;
        if(myargv[0] != NULL && strcmp(myargv[0], "ls") == 0)
        {
            myargv[i++] = (char*)"--color=auto";
        }
        while(myargv[i++] = strtok(NULL, " "));

        //如果是cd命令,不需要创建子进程,让shell自己执行对应的命令,本质就是执行系统接口
        if(myargv[0] != NULL && strcmp(myargv[0], "cd") == 0)
        {
            if(myargv[1] != NULL)
                chdir(myargv[1]);
            continue;
        }

#ifdef DEBUG
        for(int i = 0;myargv[i];i++)
        {
            printf("myargv[%d]: %s\n", i, myargv[i]);
        }
#endif
        pid_t id = fork();
        assert(id != -1);

        if(id == 0)
        {
            execvp(myargv[0], myargv);
            exit(1);
        }
        waitpid(id, NULL, 0);
    }
}
